package com.xbfinance.portal.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xbfinance.portal.dao.P2pProjectMapper;
import com.xbfinance.portal.pojo.po.P2pProject;
import com.xbfinance.portal.pojo.po.P2pProjectExample;
import com.xbfinance.portal.service.SearchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SearchServiceImpl implements SearchService {

    @Autowired
    P2pProjectMapper p2pProjectMapper;

    @Override
    public PageInfo<P2pProject> searchProject(String proname, Integer page, Integer limit) {
        PageHelper.startPage(page,limit);
        List<P2pProject> list = new ArrayList<>();
        P2pProjectExample example = new P2pProjectExample();
        example.createCriteria().andPronameLike("%"+proname+"%");
        list = p2pProjectMapper.selectByExample(example);
        PageInfo<P2pProject> pageInfo = new PageInfo<>(list,2);
        return pageInfo;
    }

    @Override
    public Map<String, Object> searchProjectOnWeb(String proname, Integer page, Integer limit) {
        System.out.println("impl:"+page+"     "+limit);
        PageHelper.startPage(page,limit);
        Map<String,Object> map = new HashMap<>();
        List<P2pProject> list = new ArrayList<>();
        P2pProjectExample example = new P2pProjectExample();
        if(proname=="" || proname==null){
            list = p2pProjectMapper.selectByExample(null);
            PageInfo<P2pProject> pageInfo = new PageInfo<>(list,2);
            map.put("pageInfo",pageInfo);
            map.put("data",list);
            map.put("count",pageInfo.getTotal());
            map.put("msg","");
            map.put("code","");
            return map;
        }
        example.createCriteria().andPronameLike("%"+proname+"%");
        list = p2pProjectMapper.selectByExample(example);
        PageInfo<P2pProject> pageInfo = new PageInfo<>(list,2);
        map.put("pageInfo",pageInfo);
        map.put("data",list);
        map.put("count",pageInfo.getTotal());
        map.put("msg","");
        map.put("code","");
        return map;
    }

    @Override
    public P2pProject queryProjectByPid(Long proid) {

        P2pProjectExample example = new P2pProjectExample();

        example.createCriteria().andProidEqualTo(proid);

        List<P2pProject> list = p2pProjectMapper.selectByExample(example);

        return list.get(0);
    }

}
